<template>
    <el-button type="primary" @click="showsave">添加</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="姓名" width="180" prop="username"></el-table-column>
      <el-table-column label="性别" width="180" prop="sex"></el-table-column>
      <el-table-column label="年龄" width="180" prop="age"></el-table-column>
     
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit1(scope.row)">
            独立编辑
          </el-button>
          <el-button size="small" @click="handleEdit(scope.row)">
            编辑
          </el-button>
          <el-popconfirm
            confirm-button-text="确认"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="是否删除?"
            @confirm="confirmEvent(scope.row.id)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹出窗体 -->
    <el-dialog v-model="dialogFormVisible" title="人员管理" width="500">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWidth">
          <el-input-number v-model="form.age" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate">
            {{ form ?.id? '更新' : '添加' }}
          </el-button>
        </div>
      </template>
    </el-dialog>
  </template>
  
  <script setup>
   import { ref,onMounted } from 'vue'
   import axios from 'axios';
   import { InfoFilled } from '@element-plus/icons-vue'
   import { ElMessage } from 'element-plus'
   import { useRouter } from 'vue-router'
import router from '../router';
 

   const dialogFormVisible=ref(false)
   const form = ref({age:1})

   //显示独立的编辑
   const handleEdit1=(row)=>{
      router.push({
        path:'/personSave',
        state:{
          params:JSON.stringify(row)
        }
      })
   }

   const showsave=()=>{
     form.value={age:1}
     dialogFormVisible.value = true;
   }
   //保存或更新
   const saveOrUpdate=()=>{
     if(form.value?.id){
      handleUpdate()
     }else{
       handleSave()
     }
   }
   //保存
   const handleSave=()=>{
    axios({
        method:'post',
        url:'/api/person.do',
        data:{"method":"add",...form.value}     
        //data:{"method":"add","username":"dd","sex":"sss","age":"11"}   
    }).then(res=>{
       dialogFormVisible.value = false;
       show()
    })
   }

   //更新
   const handleUpdate = () => {
     axios({
         method:'post',
         url:'/api/person.do',
         data:{"method":"update",...form.value}     
     }).then(res=>{
       dialogFormVisible.value = false;
       show()
     })
     dialogFormVisible.value = true;
   };
   //编辑获取数据
   const handleEdit = (row) => {
     dialogFormVisible.value = true;
     console.log(row);
     form.value={...row}
   }

   //数据源
   const tableData = ref([])
   //组件加载后自动加载数据
   const show=()=>{
    axios({
        method:'post',
        url:'/api/person.do',
        data:{"method":"findAll"}        
        }).then(({data})=>{
          let res = data
          tableData.value = res.data.list;
        })
   }
   onMounted(()=>{
    /* axios({
        method:'post',
        url:'/api/person.do',
        data:{"method":"findAll"}        
        }).then(res=>{
            tableData.value = res.data.data.list
        }) */
        show()   
  })

  //删除
  const confirmEvent = (id) => {
    axios({
      method:'post',
      url:'/api/person.do',
      data:{"method":"delete","id":id}      
    }).then(()=>{
        ElMessage({
        message: '删除成功.',
        type: 'success',
      })
        show()
    })
  }
  const cancelEvent = () => {
    ElMessage({
      message: '警告，取消了删除操作.',
      type: 'warning',
    })
  }

  </script>